<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="condition">
        <el-form-item>
          <el-select
            v-model="condition.AR_number"
            placeholder="区域编号"
            @change="areaChange(condition.AR_number, 'search')"
            clearable
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="condition.SSL_number"
            placeholder="宿舍楼编号"
            @change="dormChange(condition.SSL_number, 'search')"
            clearable
          >
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="condition.SSL_floor"
            placeholder="楼层"
            @change="floorChange(condition.SSL_floor, 'search')"
            clearable
          >
            <el-option
              v-for="item in floor"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="condition.SS_number" placeholder="房间号" clearable>
            <el-option
              v-for="item in SS_number"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 操作栏 -->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button size="small" type="success" @click="newFormVisible = true">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="success" @click="outExcel">导出EXCEL</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 数据展示 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="Loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="AR_name" fixed min-width="100"></el-table-column>
      <el-table-column align="center" label="宿舍楼名称" prop="SSL_name" min-width="80"></el-table-column>
      <el-table-column align="center" label="房间名称" prop="SS_number" min-width="80"></el-table-column>
      <el-table-column align="center" label="资产名称" prop="SS_property_description" min-width="100"></el-table-column>
      <el-table-column align="center" label="资产编号" prop="SS_property_number" min-width="100"></el-table-column>
      <el-table-column align="center" label="单价" prop="SS_price" min-width="60"></el-table-column>
      <el-table-column align="center" label="赔偿价格" prop="SS_compensation_price" min-width="60"></el-table-column>
      <el-table-column align="center" label="资产数量" prop="SS_property_tnumber" min-width="50"></el-table-column>
      <el-table-column align="center" label="备注" prop="SS_property_remarks" min-width="150"></el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!-- 新建 -->
    <el-dialog
      title="新增"
      :visible.sync="newFormVisible"
      :close-on-click-modal="false"
      class="newDialog"
    >
      <el-form label-width="6rem" ref="newForm" :model="newGoods" :rules="rule">
        <el-form-item label="所属区域" prop="AR_number">
          <el-select
            v-model="newGoods.AR_number"
            placeholder="区域编号"
            @change="areaChange(newGoods.AR_number, 'new')"
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属宿舍楼" prop="SSL_number">
          <el-select
            v-model="newGoods.SSL_number"
            placeholder="宿舍楼编号"
            @change="dormChange(newGoods.SSL_number, 'new')"
          >
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属楼层" prop="SSL_floor">
          <el-select
            v-model="newGoods.SSL_floor"
            placeholder="楼层编号"
            @change="floorChange(newGoods.SSL_floor, 'new')"
          >
            <el-option
              v-for="item in floor"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间号" prop="SS_number">
          <el-select v-model="newGoods.SS_number" placeholder="房间编号">
            <el-option
              v-for="item in SS_number"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 重复行 -->
        <el-row>
          <el-col :span="4">
            <el-form-item
              v-for="(goods, index) in newGoods.goods"
              label="物品名称"
              :key="goods.key"
              :prop="'goods.' + index + '.SS_property_description'"
              :rules="{required: true, message: '不能为空', trigger: 'blur'}"
            >
              <el-input
                v-model="goods.SS_property_description"
                style="width:8rem"
                placeholder="用品名称"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item
              v-for="(goods, index) in newGoods.goods"
              label="物品编码"
              :key="goods.key"
              :prop="'goods.' + index + '.SS_property_number'"
              :rules="{required: true, message: '不能为空', trigger: 'blur'}"
            >
              <el-input
                v-model="goods.SS_property_number"
                style="width:8rem"
                placeholder="用品编码"
                @blur="checkGoodsName(goods.SS_property_number,index)"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item
              v-for="(goods, index) in newGoods.goods"
              label="单价"
              :key="goods.key"
              :prop="'goods.' + index + '.SS_price'"
              :rules="[{required: true, message: '不能为空', trigger: 'blur'},{ type: 'number', message: '值必须为数字值'}]"
            >
              <el-input v-model.number="goods.SS_price" placeholder="单价" style="width:8rem"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item
              v-for="(goods, index) in newGoods.goods"
              label="赔偿价格"
              :key="goods.key"
              :prop="'goods.' + index + '.SS_compensation_price'"
              :rules="[{required: true, message: '不能为空', trigger: 'blur'},{ type: 'number', message: '值必须为数字值'}]"
            >
              <el-input
                v-model.number="goods.SS_compensation_price"
                placeholder="赔偿价格"
                style="width:8rem"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item
              v-for="(goods, index) in newGoods.goods"
              label="数量"
              :key="goods.key"
              :prop="'goods.' + index + '.SS_property_tnumber'"
              :rules="[{required: true, message: '不能为空', trigger: 'blur'},{ type: 'number', message: '值必须为数字值'}]"
            >
              <el-input
                v-model="goods.SS_property_tnumber"
                disabled
                placeholder="数量"
                style="width:5rem"
              ></el-input>&nbsp;&nbsp;&nbsp;&nbsp;
              <el-button @click.prevent="removeGoods(goods)" size="small" type="danger">删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="addGoods" type="success">新增资产</el-button>
        <el-button @click="resetNewForm('newForm')">重置</el-button>
        <el-button type="primary" @click="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="alterForm" :model="alterGoods" :rules="rule">
        <el-form-item label="所属区域">
          <el-input v-model="alterGoods.AR_number" disabled placeholder="用品名称"></el-input>
        </el-form-item>
        <el-form-item label="所属区域">
          <el-input v-model="alterGoods.SSL_number" disabled placeholder="用品名称"></el-input>
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="alterGoods.SS_number" disabled placeholder="用品名称"></el-input>
        </el-form-item>
        <el-form-item label="用品名称" prop="SS_property_description">
          <el-input v-model="alterGoods.SS_property_description" placeholder="用品名称"></el-input>
        </el-form-item>
        <el-form-item label="资产编码">
          <el-input v-model="alterGoods.SS_property_number" disabled placeholder="资产编码"></el-input>
        </el-form-item>
        <el-form-item label="单价" prop="SS_price">
          <el-input v-model="alterGoods.SS_price" placeholder="单价"></el-input>
        </el-form-item>
        <el-form-item label="赔偿价格" prop="SS_compensation_price">
          <el-input v-model="alterGoods.SS_compensation_price" placeholder="赔偿价格"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="alterGoods.SS_property_tnumber" disabled placeholder="数量"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="SS_property_remarks">
          <el-input
            v-model="alterGoods.SS_property_remarks"
            placeholder="备注"
            type="textarea"
            autosize
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="alterSubmit('alterForm')"
          :loading="alterLoading"
        >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { fixedassets_select } from "../../api/roomgoods";
import { area_select } from "../../api/area.js";
import { building_select } from "../../api/floor.js";
export default {
  data() {
    // 进行数字验证
    var checkNumber = (rule, value, callback) => {
      var re = /^[0-9]+.?[0-9]*$/;
      if (!value) {
        return callback(new Error("值不能为空"));
      }
      setTimeout(() => {
        if (!re.test(value)) {
          callback(new Error("请输入数字值"));
        } else {
          callback();
        }
      }, 100);
    };
    return {
      condition: {
        AR_number: null,
        SSL_number: null,
        SSL_floor: null,
        SS_number: null,
        page: 1,
      },
      tableData: [
        {
          AR_number: null,
          affiliationNumber: null,
          SSL_number: null,
          dormNumber: null,
          SS_number: null,
          SS_property_description: null,
          SS_property_number: null,
          SS_price: null,
          SS_compensation_price: null,
          SS_property_tnumber: null,
          _type: null,
          _date: null,
          floor: null,
          SS_property_remarks: null,
        },
      ],
      newGoods: {
        AR_number: null,
        SSL_number: null,
        SSL_floor: null,
        SS_number: null,
        goods: [
          {
            SS_property_description: null,
            SS_property_number: null,
            SS_price: null,
            SS_compensation_price: null,
            SS_property_tnumber: 1,
            SS_property_remarks: null,
          },
        ],
      },
      alterGoods: {
        _affiliationName: null,
        SSL_number: null,
        SS_number: null,
        SS_property_description: null,
        SS_property_number: null,
        SS_price: null,
        SS_compensation_price: null,
        SS_property_tnumber: null,
        SS_property_remarks: null,
      },
      area: [],
      dorm: [],
      floor: [],
      SS_number: [],
      rule: {
        AR_number: [
          { required: true, message: "请选择区域", trigger: "change" },
        ],
        SSL_number: [
          { required: true, message: "请选择宿舍楼", trigger: "change" },
        ],
        SSL_floor: [
          { required: true, message: "请选择楼层", trigger: "change" },
        ],
        SS_number: [
          { required: true, message: "请选择房间", trigger: "change" },
        ],
        SS_property_description: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        SS_price: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
            validator: checkNumber,
          },
        ],
        SS_compensation_price: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
            validator: checkNumber,
          },
        ],
        SS_property_remarks: [{ trigger: "blur" }],
      },
      Loading: false,
      newFormVisible: false,
      newLoading: false,
      alterFormVisible: false,
      alterLoading: false,
      currentPage: 1,
      totalPage: 1,
    };
  },
  methods: {
    getArea() {
      area_select({ AR_number: "", AR_name: "", page: 1 }).then((res) => {
        if (res) {
          for (let index = 0; index < res.data.length; index++) {
            this.area.push({
              label: res.data[index].AR_name,
              value: res.data[index].AR_number,
            });
          }
        }
      });
    },
    // 选完区域后触发宿舍楼
    areaChange() {
      if (this.condition.AR_number === "") {
        return true;
      }
      // console.log(this.condition._area);
      building_select({
        AR_number: this.condition.AR_number,
        SSL_number: "",
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          this.dorm = [];
          for (let index = 0; index < res.data.length; index++) {
            this.dorm.push({
              label: res.data[index].SSL_name,
              value: res.data[index].SSL_number,
            });
          }
        } else {
          this.dorm = [
            {
              value: "",
              label: "",
            },
          ];
          // this.condition._dorm = "";
        }
      });
    },
    //获取宿舍楼对应最多楼层
    dormChange(dorm) {
      if (dorm === "") {
        return true;
      }
      // 获取当前楼层
      building_select({
        AR_number: this.condition.AR_number,
        SSL_number: this.condition.SSL_number,
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          this.floor = []; //每次选择楼层号都清除上一次已填入floor的信息
          let maxfloor = res.data[0].SSL_floor;
          // console.log(maxfloor);
          for (let i = 1; i <= maxfloor; i++) {
            this.floor.push({
              label: i,
              value: i,
            });
          }
        }
      });
    },
    // 选完楼层后触发房间
    floorChange(floor, type) {},
    getData() {
      this.Loading = true;
      fixedassets_select(this.condition).then((res) => {
        this.Loading = false;
        if (res) {
          this.totalPage = res.page;
          this.tableData = res.data;
        } else {
          this.tableData = null;
          this.totalPage = 0;
          this.currentPage = 0;
        }
      });
    },
    // 查询
    search() {
      this.condition.page = 1;
      this.getData();
    },
    // 分页
    currentChange(val) {
      this.condition.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    // 新建
    newSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.newLoading = true;
          newRoomGoods(this.newGoods).then((res) => {
            this.$message.success(res.msg);
            this.$refs[formName].resetFields();
            this.newLoading = false;
            this.getData();
          });
        }
      });
    },
    // TODO: 导出
    outExcel() {
      window.location.href =
        "http://192.168.10.225:80/dormitoryA/Property/Property/roomGoodsExcelOut";
    },
    // 点击编辑
    handleEdit(index, row) {
      this.alterFormVisible = true;
      this.alterGoods = Object.assign({}, row);
    },
    //修改
    alterSubmit(forName) {
      this.$refs[forName].validate((valid) => {
        if (valid) {
          this.alterLoading = true;
          altertRoomGoods(this.alterGoods).then((res) => {
            this.alterLoading = false;
            if (res) {
              this.$message.success(res.msg);
            }
            this.getData();
            this.alterFormVisible = false;
            return true;
          });
        } else {
          this.$message.error("请将信息填写完整!");
          return false;
        }
      });
    },
    // 点击删除
    handleDel(row) {
      this.$confirm("此操作将永久删除该物品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteRoomGoods(row.serial).then((res) => {
            if (res) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    addGoods() {
      this.newGoods.goods.push({
        SS_property_description: null,
        SS_property_number: null,
        SS_price: null,
        SS_compensation_price: null,
        SS_property_tnumber: 1,
        key: Date.now(),
      });
    },
    removeGoods(item) {
      var index = this.newGoods.goods.indexOf(item);
      if (index !== -1) {
        this.newGoods.goods.splice(index, 1);
      }
    },
    resetNewForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 防止物品编号重复
    checkGoodsName(SS_property_number, index) {
      // 先检验是否与重复行中的相同
      this.Loading = true;
      for (let i = 0; i < this.newGoods.goods.length; i++) {
        if (this.newGoods.goods[i].SS_property_number === SS_property_number) {
          if (i !== index) {
            this.Loading = false;
            this.$message.error("编码重复!");
            this.newGoods.goods[index].SS_property_number = "";
            return false;
          }
        }
      }
      // 与已存在的编码进行验证
      checkNumberRepeat(SS_property_number, "room").then((res) => {
        if (!res) {
          this.Loadingg = false;
          this.$message.error("编码重复!");
          this.newGoods.goods[index].SS_property_number = "";
          return false;
        } else {
          this.Loading = false;
          return true;
        }
      });
    },
  },
  mounted() {
    this.getArea();
    this.getData();
  },
};
</script>
<style lang="">
.newDialog {
  width: 150%;
  margin: auto;
  left: -25rem;
}
</style>